/* --------------------------------------
SUMMARY: 	- Estimating discete choice models
			- Exporting the estimated parameters to a new dataset
AUTHOR: Thimo De Schouwer (KU LEUVEN)
NOTES: 		- Preference / WTP coefficients vary over wage distribution
DATE CREATED: 16/09/2021
STATA VERSION: MP 17.0
--------------------------------------- */

clear all
drop _all

* Part 2 - Estimating preferences for job attributes
cd "${dir}\DataCode\Data\Constructed\LISS"
import delimited Clean_LISS.txt, clear



*=========================*
* 1. Reshaping data 	  *
*=========================*
* Job A characterized by: whA flA tcA mwA mwageA
* Job B characterization differs by experiment
* 2.1 First reshape: one observation per experiment

** JOB A (based on actual job)
forvalues i = 1/10{
	gen tca`i' = telecommutea
	gen wha`i' = workhoursa
	gen fla`i' = flexibilitya
	gen mwa`i' = meaninga
	gen hwagea`i' = hrlywagea
	gen mwagea`i' = monthlywagea
}

** JOB B (differs by experiment)
* 1. Workhours
foreach i in 2 3 4 8 9 10{
	gen whb`i' = workhoursa
}

* 2. Flexibility
foreach i in 1 3 4 5 7 10{
	gen flb`i' = flexibilitya
}

* 3. Meaning
foreach i in 4 5 8 10{
	gen mwb`i' = meaningb
}

foreach i in 1 2 3 6 7 9{
	gen mwb`i' = meaninga
}

* 4. Telecommute
foreach i in 3 7 9 10{
	gen tcb`i' = telecommuteb
}

foreach i in 1 2 4 5 6 8 {
	gen tcb`i' = telecommutea
}

* 5. Monthly Wages & Hourly Wages
forvalues i = 1/10{
	gen mwageb`i' = wage_b`i'
}

forvalues i = 1/10{
	gen hwageb`i' = mwageb`i' / (whb`i'*(52/12))
}


* 6. Choices
forvalues i = 1/10{
	gen choicea`i' = 1 if hce`i' == 1
		replace choicea`i' =  0 if hce`i' == 2
	gen choiceb`i' = 1 if hce`i' == 2
		replace choiceb`i' =  0 if hce`i' == 1
}


reshape long wha whb mwagea mwageb hwagea hwageb mwa mwb tca tcb fla flb choicea choiceb, i(nomem_encr) j(experiment)

* Robustness: Remove Dominated Choices
gen equalAB = 0
replace equalAB = 1 if (wha == whb) & (mwagea == mwageb) & (mwa == mwb) & (tca == tcb) & (fla == flb)

gen no_exp = (mwageb == .)
drop if no_exp

gen dominantA = 0
replace dominantA= 1 if (mwagea >= mwageb) & (mwa >= mwb) & (tca >= tcb) & (fla >= flb) & (equalAB != 1) 
replace dominantA = 0 if (wha != whb)

gen dominantB = 0
replace dominantB = 1 if (mwagea <= mwageb) & (mwa <= mwb) & (tca <= tcb) & (fla <= flb) & (equalAB != 1)
replace dominantB = 0 if (wha != whb)

gen choose_dominated = 0
replace choose_dominated = 1 if (dominantA == 1 & choicea == 0) 
replace choose_dominated = 1 if (dominantB == 1 & choiceb == 0)

* Drop people who rushed through or made "bad choices" -> all results stay the same (do not drop for main analysis)
bysort nomem_encr: egen bad_choice = max(choose_dominated)
drop if bad_choice | duur < 180

* 1.2 Second reshape: one observation per choice
reshape long wh mw tc fl choice hwage mwage, i(nomem_encr experiment) j(choicealt a b) string
sort nomem_encr experiment

* Generating log wages and earnings
gen ln_mwage = ln(mwage)
gen ln_hwage = ln(hwage)

* Binary flexibility and hour categories
recode wh (38 = 0) (32 = 1) (20 = 2)
recode fl (0 = 0) (1 2 3 = 1)


drop if choice == .		// drop experiments that weren't conducted

*=======================================*
* 2. 	Construct Interaction Terms		*
*=======================================*
* Interaction terms: Job Attribute x Young Child
gen child = (nrchild_hh > 0)
	replace child = . if nrchild_hh == .

gen child_wh1 = (child * wh) if wh == 1
	replace child_wh1 = 0 if child_wh1 == .
	
gen child_wh2 = (child * wh) if wh == 2
	replace child_wh2 = 0 if child_wh2 == .
	
gen child_fl = child * fl
gen child_tc = tc * child
gen child_mw = mw * child


label var child_wh1 "Child $\times$ Long PT"
label var child_wh2 "Child $\times$ Short PT"
label var child_tc "Child $\times$ Telecommute"
label var child_mw "Child $\times$ Meaning"
label var child_fl "Child $\times$ Schedule Freedom"


global interactions child_wh1 child_wh2 child_tc child_mw child_fl



*===========================================================*
* 3. 	Estimating Discrete Choice Models 					*
*		Preferences Vary over Wage Distribution			  	*
*===========================================================*
drop if choice == .		// drop empty experiments that weren't conducted

* Declare choice method data
cmset nomem_encr experiment choicealt

* Mean wages by gender
forvalues g = 0/1{
	mean mwage if woman == `g'
	gl meanwage_`g' = r(table)[1,1]
}


* Estimating the model (NB: equivalent to not doing second recast and estimating logit choiceA on difference in amenities between jobs)
forvalues g = 0/1{
	eststo coefs_`g': cmclogit choice fl tc mw ln_hwage i.wh $interactions if woman == `g', vce(cluster nomem_encr)
	
		* WTP in Euro: no children
		eststo EUR_WTP_SE_nc_`g': nlcom (EUR_WTP_SE_nc_fl_`g': (1 - exp(- (_b[fl]) / _b[ln_hwage])) * ${meanwage_`g'}) ///
				(EUR_WTP_SE_nc_tc_`g': (1 - exp(- (_b[tc] / _b[ln_hwage]))) * ${meanwage_`g'}) ///
				(EUR_WTP_SE_nc_mw_`g': (1 - exp(- (_b[mw] / _b[ln_hwage]))) * ${meanwage_`g'}) ///
				(EUR_WTP_SE_nc_wh1_`g': (1 - exp(- (_b[1.wh] / _b[ln_hwage]))) * ${meanwage_`g'}) ///
				(EUR_WTP_SE_nc_wh2_`g': (1 - exp(- (_b[2.wh] / _b[ln_hwage]))) * ${meanwage_`g'}), post
		
		est restore coefs_`g'
		* WTP in Euro: children
		eststo EUR_WTP_SE_c_`g': nlcom (EUR_WTP_SE_c_fl_`g': (1 - exp(- (_b[fl] + _b[child_fl]) / _b[ln_hwage])) * ${meanwage_`g'}) ///
				(EUR_WTP_SE_c_tc_`g': (1 - exp(- ((_b[tc] + _b[child_tc]) / _b[ln_hwage]) )) * ${meanwage_`g'}) ///
				(EUR_WTP_SE_c_mw_`g': (1 - exp(- ((_b[mw] + _b[child_mw]) / _b[ln_hwage]) )) * ${meanwage_`g'}) ///
				(EUR_WTP_SE_c_wh1_`g': (1 - exp(- ((_b[1.wh] + _b[child_wh1]) / _b[ln_hwage]) )) * ${meanwage_`g'}) ///
				(EUR_WTP_SE_c_wh2_`g': (1 - exp(- ((_b[2.wh] + _b[child_wh2]) / _b[ln_hwage]) )) * ${meanwage_`g'}), post
		
		est restore coefs_`g'		
		* WTP in Euro: no children
		eststo WTP_SE_nc_`g': nlcom (EUR_WTP_SE_nc_fl_`g': (1 - exp(- (_b[fl]) / _b[ln_hwage]))) ///
				(WTP_SE_nc_tc_`g': (1 - exp(- (_b[tc] / _b[ln_hwage])))) ///
				(WTP_SE_nc_mw_`g': (1 - exp(- (_b[mw] / _b[ln_hwage])))) ///
				(WTP_SE_nc_wh1_`g': (1 - exp(- (_b[1.wh] / _b[ln_hwage])))) ///
				(WTP_SE_nc_wh2_`g': (1 - exp(- (_b[2.wh] / _b[ln_hwage])))), post
		
		* WTP in Euro: children
		est restore coefs_`g'
		eststo WTP_SE_c_`g': nlcom (EUR_WTP_SE_c_fl_`g': (1 - exp(- (_b[fl] + _b[child_fl]) / _b[ln_hwage]))) ///
				(WTP_SE_c_tc_`g': (1 - exp(- ((_b[tc] + _b[child_tc]) / _b[ln_hwage]) ))) ///
				(WTP_SE_c_mw_`g': (1 - exp(- ((_b[mw] + _b[child_mw]) / _b[ln_hwage]) ))) ///
				(WTP_SE_c_wh1_`g': (1 - exp(- ((_b[1.wh] + _b[child_wh1]) / _b[ln_hwage]) ))) ///
				(WTP_SE_c_wh2_`g': (1 - exp(- ((_b[2.wh] + _b[child_wh2]) / _b[ln_hwage]) ))), post
				
				
		** Save WTP in variable (for later: calculation of equivalent wages)
		est restore coefs_`g'
			predictnl wtp_`g'tc = 1 - exp(- (_b[tc]  + _b[child_tc] * child) / _b[ln_hwage]) if woman == `g', ci(wtp_`g'tc_ub wtp_`g'tc_lb)
			predictnl wtp_`g'mw = 1 - exp(- (_b[mw]  + _b[child_mw] * child) / _b[ln_hwage]) if woman == `g', ci(wtp_`g'mw_ub wtp_`g'mw_lb)
			predictnl wtp_`g'fl = 1 - exp(- (_b[fl]  + _b[child_fl] * child) / _b[ln_hwage]) if woman == `g', ci(wtp_`g'fl_ub wtp_`g'fl_lb)
			predictnl wtp_`g'wh1 = 1 - exp(- (_b[1.wh] + _b[child_wh1] * child) / _b[ln_hwage]) if woman == `g', ci(wtp_`g'wh1_ub wtp_`g'wh1_lb)
			predictnl wtp_`g'wh2 = 1 - exp(- (_b[2.wh]  + _b[child_wh2] * child) / _b[ln_hwage]) if woman == `g', ci(wtp_`g'wh2_ub wtp_`g'wh2_lb)

}



** Table: WTP in % and levels by gender
esttab WTP_SE_nc_0 WTP_SE_nc_1 EUR_WTP_SE_nc_0 EUR_WTP_SE_nc_1 using "$dir\GraphsTables\OnlineAppendix\Table_ND_WTP_Gender_NC.txt" , varwidth(25) modelwidth(15) b(%5.3f) ///
	rename(WTP_SE_nc_fl_1 "Schedule Adaptability" WTP_SE_nc_fl_0 "Schedule Adaptability" EUR_WTP_SE_nc_fl_1 "Schedule Adaptability" EUR_WTP_SE_nc_fl_0 "Schedule Adaptability" WTP_SE_nc_tc_1 Telecommuting WTP_SE_nc_tc_0 Telecommuting EUR_WTP_SE_nc_tc_1 Telecommuting EUR_WTP_SE_nc_tc_0 Telecommuting WTP_SE_nc_mw_1 Meaning WTP_SE_nc_mw_0 Meaning EUR_WTP_SE_nc_mw_1 Meaning EUR_WTP_SE_nc_mw_0 Meaning WTP_SE_nc_wh1_1 "Long Part-Time" WTP_SE_nc_wh1_0 "Long Part-Time" EUR_WTP_SE_nc_wh1_1 "Long Part-Time" EUR_WTP_SE_nc_wh1_0 "Long Part-Time" WTP_SE_nc_wh2_1 "Short Part-Time" WTP_SE_nc_wh2_0 "Short Part-Time" EUR_WTP_SE_nc_wh2_1 "Short Part-Time" EUR_WTP_SE_nc_wh2_0 "Short Part-Time") ///
	nonote ///
    eqlabels(none) collabels(none) mgroups("WtP (\% wage)" "WtP (monthly income, €)", pattern(0 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles("Men" "Women" "Men" "Women") se nogaps nofloat nonumbers replace star(* 0.05) tex

esttab WTP_SE_c_0 WTP_SE_c_1 EUR_WTP_SE_c_0 EUR_WTP_SE_c_1 using "$dir\GraphsTables\OnlineAppendix\Table_ND_WTP_Gender_C.txt", varwidth(25) modelwidth(15) b(%5.3f) ///
	rename(WTP_SE_c_fl_1 "Schedule Adaptability" WTP_SE_c_fl_0 "Schedule Adaptability" EUR_WTP_SE_c_fl_1 "Schedule Adaptability" EUR_WTP_SE_c_fl_0 "Schedule Adaptability" WTP_SE_c_tc_1 Telecommuting WTP_SE_c_tc_0 Telecommuting EUR_WTP_SE_c_tc_1 Telecommuting EUR_WTP_SE_c_tc_0 Telecommuting WTP_SE_c_mw_1 Meaning WTP_SE_c_mw_0 Meaning EUR_WTP_SE_c_mw_1 Meaning EUR_WTP_SE_c_mw_0 Meaning WTP_SE_c_wh1_1 "Long Part-Time" WTP_SE_c_wh1_0 "Long Part-Time" EUR_WTP_SE_c_wh1_1 "Long Part-Time" EUR_WTP_SE_c_wh1_0 "Long Part-Time" WTP_SE_c_wh2_1 "Short Part-Time" WTP_SE_c_wh2_0 "Short Part-Time" EUR_WTP_SE_c_wh2_1 "Short Part-Time" EUR_WTP_SE_c_wh2_0 "Short Part-Time") ///
	addnote("Note: standard errors clustered on the individual level and calculated with delta method.") ///
    eqlabels(none) collabels(none) mgroups("WtP (\% wage)" "WtP (monthly income, €)", pattern(0 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles("Men" "Women" "Men" "Women") se nogaps nofloat nonumbers replace star(* 0.05) tex
*/
